package modele;

import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DAO {

    private static final String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
    private static final String url2 = "http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/";

    public static List<String> getLesDeps() {
        List<String> lesDeps = new ArrayList<String>();
        try {

            URL myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());

            Element racine = doc.getDocumentElement();

            NodeList listeDep = racine.getElementsByTagName("Departement");

            for (int i = 0; i < listeDep.getLength(); i++) {

                Node departement = listeDep.item(i);
                NodeList lesProprietes = departement.getChildNodes();

                for (int j = 0; j < lesProprietes.getLength(); j++) {

                    if (lesProprietes.item(j).getNodeName().equals("num")) {

                        lesDeps.add(lesProprietes.item(j).getTextContent().trim());
                        break;

                    }

                }

            }

        } catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lesDeps;
    }

    public static List<Medecin> getLesMeds(String leDep) {
        List<Medecin> lesMeds = new ArrayList<Medecin>();
        try {
            String urlMed = url2 + leDep;
            URL myURL = new URL(urlMed);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());

            Element racine = doc.getDocumentElement();

            NodeList listeMed = racine.getElementsByTagName("Medecin");

            for (int i = 0; i < listeMed.getLength(); i++) {

                Node medecin = listeMed.item(i);
                NodeList lesProprietes = medecin.getChildNodes();

                String nom="";
                String prenom="";
                String adresse="";
                String tel="";
                String spe="";
                String id="";
                
                for (int j = 0; j < lesProprietes.getLength(); j++) {

                    if (lesProprietes.item(j).getNodeName().equals("nom")) {

                        nom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("prenom")) {

                        prenom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("adresse")) {

                        adresse = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("tel")) {

                        tel = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("spe")) {

                        spe = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("id")) {

                        id = lesProprietes.item(j).getTextContent().trim();
                    }
                }
                Medecin m = new Medecin(nom, prenom, adresse, tel, spe, id);
                lesMeds.add(m);

            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lesMeds;
    }

}
